const Service = require('egg').Service
const { Op } = require("sequelize");

class SwiperService extends Service{
   async getSwiper() {
    const {ctx} = this
    let swiperList = await ctx.model.Image.findAll()
    console.log(swiperList);
    let ids = []
    for(let i=0;i<=3;i++) {
      let num = Math.ceil((Math.random())*swiperList.length)
        if(ids.indexOf(num)==-1) {
          ids.push(num)
        }else{
          i--
        }
    }
    // console.log(ids);
    // return swiperList
    let arr = []
    for(let i=0;i<ids.length;i++) {
      // console.log(quoteList[ids[i]]);
      let index = ids[i]-1
      arr.push(swiperList[index].dataValues)
    }
    // console.log(arr);
    return arr
  }

  async getSwiperList() {
    const {ctx} = this
    return await ctx.model.Image.findAll()
  }

  async postSwiper(body) {
    const {ctx} = this
    return await ctx.model.Image.create({
      url:body.data
    })
  }

  async deleteSwiper(id) {
    const {ctx} = this
    return await ctx.model.Image.destroy({
      where:{
        id
      }
    })

  }
}

module.exports = SwiperService